package com.zxp.zxpsmarthomeapk.view.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zxp.zxpsmarthomeapk.R;
import com.zxp.zxpsmarthomeapk.circlrimageview.CircleImageView;
import com.zxp.zxpsmarthomeapk.model.UserInfoModle;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserFragment extends Fragment {
    @BindView(R.id.rv_user_info)
    RecyclerView rv_user_info;

    @BindView(R.id.circle_imageView_user)
    CircleImageView circle_imageView_user;

    private View view_user;
    private List<UserInfoModle> userInfoModleList;

    public UserFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view_user = inflater.inflate(R.layout.fragment_user, container, false);
        ButterKnife.bind(this, view_user);
        initView();
        readImage();
        return view_user;
    }

    private void initView() {
        userInfoModleList = new ArrayList<UserInfoModle>();
        UserInfoModle userInfoModle0 = new UserInfoModle("我的家人", R.mipmap.img_family);
        UserInfoModle userInfoModle1 = new UserInfoModle("监控记录", R.mipmap.img_watch);
        UserInfoModle userInfoModle2 = new UserInfoModle("城市定位", R.mipmap.img_location);
        UserInfoModle userInfoModle3 = new UserInfoModle("设置", R.drawable.ic_settings_black_24dp);
        UserInfoModle userInfoModle4 = new UserInfoModle("帮助与支持", R.mipmap.img_about);
        UserInfoModle userInfoModle5 = new UserInfoModle("意见反馈", R.mipmap.img_message);
        UserInfoModle userInfoModle6 = new UserInfoModle("关于SmartHome", R.mipmap.img_imformation);
        UserInfoModle userInfoModle7 = new UserInfoModle("退出当前账号", R.mipmap.img_exit);

        userInfoModleList.add(userInfoModle0);
        userInfoModleList.add(userInfoModle1);
        userInfoModleList.add(userInfoModle2);
        userInfoModleList.add(userInfoModle3);
        userInfoModleList.add(userInfoModle4);
        userInfoModleList.add(userInfoModle5);
        userInfoModleList.add(userInfoModle6);
        userInfoModleList.add(userInfoModle7);

        rv_user_info.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_user_info.setAdapter(new CommonAdapter<UserInfoModle>(getActivity(), R.layout.rv_user_info_item, userInfoModleList) {
            @Override
            protected void convert(final ViewHolder holder, final UserInfoModle mainItemModel, final int position) {
                holder.setText(R.id.tv_item, mainItemModel.getTv_item());
                holder.setImageResource(R.id.iv_item, mainItemModel.getImage_item());
                holder.setOnClickListener(R.id.iv_item, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(getContext(), "222", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        rv_user_info.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void onResume() {
        super.onResume();
        if (readImage()) {
            return;
        }
    }

    //如果本地有,就不需要再去联网去请求
    private boolean readImage() {
        File filesDir;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //判断sd卡是否挂载
            //路径1：storage/sdcard/Android/data/包名/files
            filesDir = getActivity().getExternalFilesDir("");

        } else {//手机内部存储
            //路径：data/data/包名/files
            filesDir = getActivity().getFilesDir();
        }
        File file = new File(filesDir, "icon.png");
        if (file.exists()) {
            //存储--->内存
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            circle_imageView_user.setImageBitmap(bitmap);
            return true;
        }
        return false;
    }
}
